<?php

namespace app\admin\controller;

use app\admin\controller\AdminBase;
use app\admin\model\Page;
use app\common\model\PermissionsModel;

/**
 * 权限管理
 *     针对前端 权限 的所有操作
 * 
 * 时间 ： 2020年4月9日14:24:15
 * 作者 ： 夏立军
 */
class Permissions extends AdminBase
{

    // 首页
    public function index()
    {
        $permissions = new PermissionsModel();
        $permissions_count = $permissions->get_permissions_count();
        $size = config('ams_config.permissions_page_size');

        $page = new Page();
        $page_res = $page->getPageRes($permissions_count, $size, url('index'));

        $permissions_list = $permissions->permissions_list_page($page_res['offset'], $page_res['size']);

        return view('index', [
            // 分类列表
            'permissions_list' => $permissions_list,
            // 若分类无数据，则输出设置的样式
            'empty' => '<tr><td colspan="8" style="text-align: center;">没有数据</td></tr>',
            // 分页的处理结果
            'page_res' => $page_res,
        ]);
    }

    // 添加一个权限
    public function permissions_add()
    {
        if (request()->post()) {
            $name = input('post.name/s', null);
            $url = input('post.url/s', null);
            $status = input('post.status/d', null);
            $note = input('post.note/s', null);

            if (is_null($name) || is_null($url) || is_null($status)) {
                return json(['code' => 1, 'msg' => '数据不完整']);
            }

            if (mb_strlen($name) < 2) {
                return json(['code' => 2, 'msg' => '权限名称不能少于2位']);
            }

            $permissions = new PermissionsModel();
            $aid = session('admin_login')->id;
            $res = $permissions->permissions_add($aid, $name, $url, $status, $note);
            if (!$res) {
                return json(['code' => 3, 'msg' => '添加失败，名称或地址重复']);
            }
            return json(['code' => 0, 'msg' => '添加成功']);
        }
        return view('permissions_add', [
            // 权限状态
            'permissions_status' => config('ams_config.permissions_status'),
        ]);
    }

    // 编辑权限
    public function permissions_edit()
    {
        if (request()->post()) {
            $id = input('post.id/d', null);
            $name = input('post.name/s', null);
            $url = input('post.url/s', null);
            $status = input('post.status/d', null);
            $note = input('post.note/s', null);

            if (is_null($id) || is_null($name) || is_null($url) || is_null($status)) {
                return json(['code' => 1, 'msg' => '填写数据不完整']);
            }

            $permissions = new PermissionsModel();
            $permissions_info = $permissions->set_permissions_info($id, $name, $url, $status, $note);

            if (!$permissions_info) {
                return json(['code' => 2, 'msg' => '修改失败，名称或地址重复']);
            }

            return json(['code' => 0, 'msg' => '修改成功']);
        }

        $pid = input('get.pid/d', null);
        if (is_null($pid)) {
            return redirect_index('角色ID不正确', url('Permissions/index'));
        }

        $permissions = new PermissionsModel();
        $permissions_info = $permissions->get_permissions_info_by_id($pid);
        if (is_null($permissions_info)) {
            return redirect_index('权限信息不存在', url('Role/index'));
        }

        return view('permissions_edit', [
            // 权限信息
            'permissions_info' => $permissions_info,
            // 权限状态
            'permissions_status' => config('ams_config.permissions_status'),
        ]);
    }

    // 删除权限
    public function permissions_del()
    {
        if (request()->post()) {
            $pid = input('post.pid/d', null);
            if (is_null($pid)) {
                return json(['code' => 1, 'msg' => 'ID 不存在']);
            }

            $permissions = new PermissionsModel();
            $permissions_info = $permissions->permissions_del($pid);
            if (!$permissions_info) {
                return json(['code' => 2, 'msg' => '删除失败']);
            }

            return json(['code' => 0, 'msg' => '删除成功']);
        }
        return json(['code' => 99, 'msg' => '请求模式错误']);
    }
}
